<?php
namespace Home\Controller;
use Home\Controller;
/**
* 用户管理
*/
class UserController extends AdminController
{
	/**
	 * 用户管理列表
	 * @return [type] [description]
	 */
	public function userManage($order = 'id',$p = 1)
	{
		$admininfo = $this->checkLogin();
		$this->assign('admininfo', $admininfo);
		//用户列表
		$Admin = D('Admin');
		$res = $Admin
			->field('bg_admin.id,bg_admin.username,bg_admin.email,bg_admin.loginnum,bg_admin.lastdate,bg_article.artid,count(*)')
			->join('bg_article ON bg_admin.id = bg_article.artid','left')
			->group('bg_article.artid')
			->order($order)
			->page($p,10)
			->select();
		$this->assign('userinfo', $res);
		//分页
		$count = $Admin->count();
		$page = new \Think\Page($count,10);
		$page->setConfig('prev','<i class="fa fa-chevron-left"></i>');
		$page->setConfig('next','<i class="fa fa-chevron-right"></i>');
		$show = $page->show();
		$this->assign('page', $show);

		//用户文章
		$Article = M('Article');
		$userArt = $Article->field('id,title,hits')->where('artid=' . session('admin.id'))->order('updatetime desc')->limit(5)->select();
		$this->assign('userArt', $userArt);
		$this->display();
	}
	/**
	 * 添加用户
	 */
	public function addUser()
	{
		if (IS_POST) {
			$Admin = D('Admin');
			if (!$Admin->create()) {
				alertMes($Admin->getError(), U('user/userManage'));
			} else {
				$arr['username'] = I('post.username');
				$arr['password'] = md5(I('post.password'));
				$arr['email'] = I('post.email');
				$res = $Admin->add($arr);
				if ($res) {
					alertMes('添加成功', U('user/userManage'));
				} else {
					alertMes('添加失败', U('/user/userManage'));
				}
			}
		} else {
			$this->redirect('/user/userManage');
		}
	}
	/**
	 * 编辑用户
	 * @param  int $id [description]
	 * @return [type]     [description]
	 */
	public function editUser($id)
	{
		$Admin = D('Admin');
		$arr['id'] = $id;
		$row = $Admin->where($arr)->field('id,username,email')->find();
		if (IS_POST) {
			if (!$Admin->create(I('post.'),2)) {
				alertMes($Admin->getError());
			} else {
				$arr['username'] = I('post.username');
				$arr['password'] = md5(I('post.password'));
				$arr['email'] = I('post.email');
				$res = $Admin->save($arr);
				if ($res) {
					alertMes('修改成功', U('user/userManage'));
				} else {
					alertMes('修改失败');
				}
			}
		} else {
			$admininfo = $this->checkLogin();
			$this->assign('admininfo', $admininfo);
			$this->assign('row', $row);
			$this->display();
		}
	}
	/**
	 * 删除用户
	 * @param  int $id [description]
	 * @return [type]     [description]
	 */
	public function delUser($id)
	{
		$this->checkLogin();
		$Admin = M('Admin');
		$oldhead = $Admin->where('id=' . $id)->field('headimg')->find();
		if (is_file($oldhead['headimg'])) {
			unlink ($oldhead['headimg']);
		}
		$res = $Admin->where('id=' . $id)->delete();
		if ($res) {
			alertMes('删除成功', U('user/userManage'));
		} else {
			alertMes('删除失败', U('user/userManage'));
		}
	}
	/**
	 * 修改头像写入数据库
	 * @param  int $id [description]
	 * @return [type]     [description]
	 */
	public function changeImg($id)
	{
		$upload = new \Think\Upload();
		$upload->maxSize = 2097152 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './App/head/'; // 设置附件上传根目录
		$upload->autoSub = false;
		$upload->savePath = ''; // 设置附件上传（子）目录
		$upload->saveName = array('uniqid', '');
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			echo $upload->getError();
		}else{// 上传成功
			$image = new \Think\Image();
			$imgAdd = 'App/head/' . $info['file']['savename'];
			$image->open($imgAdd);
			$savePath = 'App/head/' . md5(time() . $info['file']['savename']) . '.jpeg';
			$image->thumb(120, 120, \Think\Image::IMAGE_THUMB_FIXED)->save($savePath);
			unlink($imgAdd);
			//删除老头像，写入新头像路径
			$Admin = M('Admin');
			$oldhead = $Admin->where('id=' . $id)->field('headimg')->find();
			if (is_file($oldhead['headimg'])) {
				unlink ($oldhead['headimg']);
			}
			$imgName['headimg'] = $savePath;
			$Admin->where('id=' . $id)->save($imgName);
			if (session('admin')) {
				session('admin.headimg', $savePath);
			} else {
				cookie('headimg', $savePath, 'expire=604800&path=' . __ROOT__ . '/');
			}
			echo $savePath;
		}
	}
}